<?php

namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;
use App\Http\Resources\ServiceResource;
use App\Models\News;
use Illuminate\Http\Request;

class NewsController extends Controller
{
    public function index(Request $request)
    {
        $categoryId = $request->category_id ?? 0;

        $resources = News::select('*')
            ->when($categoryId, function ($query) use ($categoryId) {
                return $query->where('type', $categoryId);
            })
            ->latest('orderby')
            ->latest('id')
            ->paginate(request()->per_page ?? 10);

        return new ServiceResource($resources);
    }

    public function show(News $news)
    {
        $news->increment('readnum');
        return $this->successful($news);
    }
}
